package lesson08;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import java.io.File;
import java.io.FilenameFilter;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

//控制器类
public class FileFilter6703Controller {

    @FXML
    private TextField tfDir;

    @FXML
    private Label lblMsg;

    @FXML
    private ListView<String> lvFiles;

    @FXML
    private RadioButton rb1;

    @FXML
    private ToggleGroup tg;

    @FXML
    private RadioButton rb2;

    @FXML
    private RadioButton rb3;

    @FXML
    void listFile(ActionEvent event) {

    }

    @FXML
    void initialize() {
        String tfDirText = tfDir.getText().trim();
        if ("".equals(tfDirText)) {
            tfDirText = ".";
        }
        lvFiles.getItems().addAll(inner6703(tfDirText));

        //监听器 监听事件
        tg.selectedToggleProperty().addListener(new ChangeListener() {

            @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                ObservableList<String> items = lvFiles.getItems();
                String selectBtn = ((RadioButton) newValue).getText();
                String tfDirText = tfDir.getText().trim();

                if ("".equals(tfDirText)) {
                    tfDirText = ".";
                }

                //窗口目录
                switch (selectBtn) {

                    case "方法一：匿名内部类":
                        items.clear();
                        items.addAll(inner6703(tfDirText));
                        break;

                    case "方法二：Lambda表达式":
                        items.clear();
                        items.addAll(lambda6703(tfDirText));
                        break;

                    case "方法三：方法引用":
                        items.clear();
                        items.addAll(reference6703(tfDirText));
                        break;

                    default:
                        break;
                }
            }
        });

        //监听器 监听事件
        tfDir.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                ObservableList<String> items = lvFiles.getItems();

                String btn = tg.selectedToggleProperty().getValue().toString();

                int btnIndex = btn.indexOf("方法");

                String selectBtn = tg.selectedToggleProperty().getValue().toString().substring(btnIndex, btn.length() - 1);
                String tfDirText = tfDir.getText().trim();

                if ("".equals(tfDirText)) {
                    tfDirText = ".";
                }


                /*
                实现思路：
                （1）检查输入的目录是否正确（File的isDirectory方法）；
                （2）对于每一种过滤条件创建FilenameFilter，使用File的listFiles方法得到目录中满足条件的文件；
                    对满足条件的结果进行统计，统计结果添加到ListView中；
                    将满足条件的文件名添加到ListView中.
                 */
                switch (selectBtn) {

                    case "方法一：匿名内部类":
                        items.clear();
                        items.addAll(inner6703(tfDirText));
                        break;

                    case "方法二：Lambda表达式":
                        items.clear();
                        items.addAll(lambda6703(tfDirText));
                        break;

                    case "方法三：方法引用":
                        items.clear();
                        items.addAll(reference6703(tfDirText));
                        break;

                    default:
                        break;
                }
            }
        });
    }

    //定义inner6703方法，使用匿名内部类实现文件过滤器接口FilenameFilter
    public static List<String> inner6703(String str) {
        File file = new File(str);
        List<String> list = new ArrayList<>();
        File[] filesone = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".java");
            }
        });
        list.add("(1)后缀为java的文件：" + filesone.length);
        for (File f : filesone) {
            list.add(f.getAbsolutePath());
        }

        File[] filestwo = file.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return new File(dir, name).length() > 2048;
            }
        });
        list.add("(2)大于2K的文件：" + filestwo.length);
        for (File f : filestwo) {
            list.add(f.getAbsolutePath());
        }

        File[] filesthree = file.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                LocalDateTime oldTime = LocalDateTime.now().plusDays(-3);
                Long milli = oldTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
                return dir.lastModified() > milli;
            }
        });

        list.add("(3)3天前到现在修改过的文件：" + filesthree.length);

        for (File f : filesthree) {
            list.add(f.getAbsolutePath());
        }
        return list;
    }

    //定义lambda6703方法：用Lambda表达式实现文件过滤器接口FilenameFilter
    public static List<String> lambda6703(String str) {

        File file = new File(str);
        List<String> list = new ArrayList<>();

        File[] filesone = file.listFiles((dir, name) -> name.endsWith(".java"));

        list.add("(1)后缀为java的文件：" + filesone.length);

        for (File f : filesone) {
            list.add(f.getAbsolutePath());
        }

        File[] filestwo = file.listFiles((dir, name) -> new File(dir, name).length() > 2048);
        list.add("(2)大于2K的文件：" + filestwo.length);
        for (File f : filestwo) {
            list.add(f.getAbsolutePath());
        }

        File[] filesthree = file.listFiles((dir, name) -> {
            LocalDateTime oldTime = LocalDateTime.now().plusDays(-3);
            Long milli = oldTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
            return dir.lastModified() > milli;
        });

        list.add("(3)3天前到现在修改过的文件：" + filesthree.length);
        for (File f : filesthree) {
            list.add(f.getAbsolutePath());
        }
        return list;
    }

    //定义reference6703方法：用方法引用实现文件过滤器接口FilenameFilter
    public static List<String> reference6703(String str) {
        File file = new File(str);
        List<String> list = new ArrayList<>();
        File[] filesone = file.listFiles(Filter6703::myEndsJava);
        list.add("(1)后缀为java的文件：" + filesone.length);
        for (File f : filesone) {
            list.add(f.getAbsolutePath());
        }

        File[] filestwo = file.listFiles(Filter6703::myOverTwo);
        list.add("(2)大于2K的文件：" + filestwo.length);
        for (File f : filestwo) {
            list.add(f.getAbsolutePath());
        }

        File[] filesthree = file.listFiles(Filter6703::myLastThree);
        list.add("(3)3天前到现在修改过的文件：" + filesthree.length);
        for (File f : filesthree) {
            list.add(f.getAbsolutePath());
        }
        return list;
    }

    static class Filter6703 {
        public static Boolean myEndsJava(File dir, String name) {
            return name.endsWith("." + "java");
        }

        public static Boolean myOverTwo(File dir, String name) {
            return new File(dir, name).length() > 2048;
        }

        public static Boolean myLastThree(File dir, String name) {
            LocalDateTime beforeTime = LocalDateTime.now().plusDays(-3);
            Long milli = beforeTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
            return dir.lastModified() > milli;
        }
    }
}